<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/6
 * Time: 5:35 PM
 */

namespace App\VO\Usercenter\UserCoupon;

use App\Models\UserCoupon;
use Illuminate\Database\Eloquent\Collection;

class IndexUserCouponsVO implements \Iterator
{
    /**
     * @var int
     */
    private $position = 0;

    /**
     * @var IndexUserCouponModelVO[]
     */
    private $userCouponsModelVO = [];

    /**
     * 订单数量
     * @var int
     */
    private $count = 0;

    /**
     * IndexUserCouponsVO constructor.
     * @param UserCoupon[] $collection
     */
    public function __construct(Collection $collection)
    {
        $this->position = 0;
        $this->count = $collection->count();

        foreach ($collection as $userCouponModel) {
            $this->userCouponsModelVO[] = new IndexUserCouponModelVO($userCouponModel);
        }
    }

    /**
     * @return int
     */
    public function count()
    {
        return $this->count;
    }

    /**
     * Return the current element
     * @link http://php.net/manual/en/iterator.current.php
     * @return mixed Can return any type.
     * @since 5.0.0
     */
    public function current()
    {
        return $this->userCouponsModelVO[$this->position];
    }

    /**
     * Move forward to next element
     * @link http://php.net/manual/en/iterator.next.php
     * @return void Any returned value is ignored.
     * @since 5.0.0
     */
    public function next()
    {
        $this->position += 1;
    }

    /**
     * Return the key of the current element
     * @link http://php.net/manual/en/iterator.key.php
     * @return mixed scalar on success, or null on failure.
     * @since 5.0.0
     */
    public function key()
    {
        return $this->position;
    }

    /**
     * Checks if current position is valid
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean The return value will be casted to boolean and then evaluated.
     * Returns true on success or false on failure.
     * @since 5.0.0
     */
    public function valid()
    {
        return isset($this->userCouponsModelVO[$this->position]);
    }

    /**
     * Rewind the Iterator to the first element
     * @link http://php.net/manual/en/iterator.rewind.php
     * @return void Any returned value is ignored.
     * @since 5.0.0
     */
    public function rewind()
    {
        $this->position = 0;
    }
}
